<template>
  <div v-show="$store.state.design.whatTool===3" class="toolbox">
    <h3 class="tooltop ovtext">{{ $store.state.design.changeText?'Edit Text':'Add Text' }}
      <div v-show="$store.state.design.changeText" class="closebtn" @click="cl" />
    </h3>
    <div class="toolboxmain">
      <input type="text" placeholder="Enter text here" :value="$store.state.design.textObj.txt" class="textipt" @change="changeText">
      <div v-show="!$store.state.design.changeText" class="addtextbtn" @click="addText">Add To Design</div>
      <div v-show="$store.state.design.changeText">
        <div class="oneopt ovtext" @click="$store.commit('design/setFontShow',true)">
          <div class="fonticobox"><img v-if="fontData" :src="fontData[$store.state.design.textObj.font].ico" alt="ico" :title="fontData[$store.state.design.textObj.font].name"></div>Font Family
        </div>
        <div class="oneopt ovtext" @click="openpantone">Text Color
          <div class="myfr">
            {{ $store.state.design.textObj.pantone1 }}<span class="onepantone" style="float:right; margin: 20px 0 0 10px;" :style="{backgroundColor:'#'+$store.state.design.textObj.fill_color}" />
          </div>
        </div>
        <div class="oneopt ovtext" @click="$store.commit('design/setOutlineShow',true)">Text Outline
          <strong v-show="!$store.state.design.textObj.stroke">Add Outline</strong>
          <div v-show="!!$store.state.design.textObj.stroke" class="myfr">
            <span style="float:left;">{{ $store.state.design.textObj.pantone2 }}</span>
            <span class="onepantone" style="margin: 20px 0 0 10px;" :style="{backgroundColor:'#'+$store.state.design.textObj.stroke}" />
            <span class="line" />
            <span class="spcspan">{{ strokeMap[$store.state.design.textObj.strokewidth] }}</span>
          </div>
        </div>
      </div>
    </div>
    <font-box :font-data="fontData" />
    <outline-box />
  </div>
</template>
<script>
import md5 from 'js-md5';
import fontBox from './font-box';
import outlineBox from './outline-box';
export default {
  components: { fontBox, outlineBox },
  data() {
    return {
      fontData: {
        '0': {
          'name': 'Lemonada',
          'fullname': 'Lemonada',
          'ico': '/fonts/lemonada.png',
          'file': '../fonts/Lemonada-Regular.ttf'
        },
        '1': {
          'name': 'Rowdies',
          'fullname': 'Rowdies',
          'ico': '/fonts/rowdies.png',
          'file': '../fonts/Rowdies-Regular.ttf'
        }
      },
      strokeMap: {
        4: 1,
        8: 2,
        10: 3,
        12: 4,
        14: 5,
        16: 6,
        20: 7
      }
    };
  },
  created() {
    var str = [globalData.uuid, this.$store.state.design.salt].sort().join('');
    var mydata = { signature: md5(str), uuid: globalData.uuid };
  },
  methods: {
    addText() {
      if (this.$store.state.design.textObj.txt) {
        var myuri = encodeURIComponent(
          JSON.stringify(this.$store.state.design.textObj)
        );
        getFontPic(this.$store.state.design.textObj, function(url) {
          console.log('url', url);
          console.log('myuri', myuri);
          svgTool.addImg(url, 'text', myuri);
        });
      }
    },
    changeText(e) {
      if (!e.target.value) {
        e.target.value = this.$store.state.design.textObj.txt;
        return;
      }
      this.$store.commit('design/setTextObj', { key: 'txt', value: e.target.value });
      // 触发更改图片时查看cancelLate状态,如果为1时则让它为状态2 (这个事件会在200毫秒内被触发)
      if (svgTool.cancelLate === 1) svgTool.cancelLate = 2;
      if (this.$store.state.design.changeText) {
        var myuri = encodeURIComponent(
          JSON.stringify(this.$store.state.design.textObj)
        );
        getFontPic(this.$store.state.design.textObj, function(url) {
          svgTool.changeImg(url, myuri);
        });
      }
    },
    openpantone() {
      this.$store.commit('design/setPantoneBoxShow', true);
      this.$store.commit('design/setChoisePantone', {
        rgb: this.$store.state.design.textObj.fill_color,
        pantone: this.$store.state.design.textObj.pantone1
      });
      this.$store.commit('design/setColorTitle', 'Font Colors');
    },
    cl() {
      svgTool.cancelFun();
    }
  }
};
</script>
<style lang="scss" scoped>
.toolbox {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background-color: #fff;
  overflow: auto;
}
.textipt{
  display: block;
  width: 405px;
  height: 47px;
  margin: 0 auto 30px;
  text-align: center;
}
.addtextbtn{
  width: 150px;
  text-align: center;
  height: 37px;
  line-height: 37px;
  color: #666;
  border: 1px solid #DBD9DA;
  cursor: pointer;
  margin: 0 20px 0 auto;
  font-weight: 600;
}
.addtextbtn:hover{
  color: #339;
  border-color: #339;
}
.closebtn{
  float: right;
  cursor: pointer;
  width: 40px;
  height: 40px;
  background: url('/static/image/cl.png') no-repeat 10px 15px;
}
.closebtn:hover{
  background-image: url('/static/image/cl1.png');
}
.onepantone{
  float: left;
  width: 26px;
  height: 26px;
  cursor: pointer;
  position: relative;
  margin: 18px 0 0 21px;
  -webkit-box-shadow: 2px 2px 2px #eee;
          box-shadow: 2px 2px 2px #eee;
}
.onepantone:hover{
  -webkit-box-shadow: 2px 2px 2px #aaa;
          box-shadow: 2px 2px 2px #aaa;
}
.toolboxmain{
  height: calc(100% - 55px);
  overflow: auto;
}
.tooltop{
  padding-top: 5px;
  height: 50px;
  line-height: 50px;
  text-indent: 20px;
  font-weight: 700;
}
.ovtext{
  overflow:hidden;
  text-overflow:ellipsis;
  white-space:nowrap;
}
.oneopt {
  height: 70px;
  border-bottom: 1px solid #dbd9da;
  line-height: 70px;
  margin: 0 20px;
  cursor: pointer;
}
.fonticobox {
  overflow: hidden;
  float: right;
  height: 50px;
  width: 300px;
  text-align: right;
  margin-top: 10px;
}
.fonticobox img {
  height: 50px;
}
.oneopt strong {
  float: right;
  color: #339;
  font-weight: 700;
}
.myfr {
  float: right;
  position: relative;
  height: 70px;
}
.line {
  float: left;
  height: 24px;
  margin: 22px 10px;
  border: 1px solid #eee;
}
.spcspan {
  font-size: 22px;
  font-weight: 500;
  color: #aaa;
}
</style>

// WEBPACK FOOTER //
// src/components/text-box.vue
